﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using APH.Parl2.Shared.Tests.ParlInfoService;

namespace APH.Parl2.Shared.Tests.ParlInfoOdataAPI
{

	class EntityQueries
	{
		private ParlInfoContainer _parlInfoEntities;

		public EntityQueries()
		{
			// TODO have this configuration driven 
			_parlInfoEntities = new ParlInfoContainer(new Uri("http://localhost:3623/ParlInfoService.svc"));
		}

		#region Bills
		/*
		 * UI-02.03 Bills Search Results
		 */
		public IQueryable<Bill> Bills(
			string keyword, 
			bool? titleOnlyKeywordSearch,
			DateTime? fromDate,
			DateTime? toDate,
			int? parliamentNumber,
			string status,
			string type)
		{
			var bills = _parlInfoEntities.Bills.AsQueryable();
			// keyword search
			if (keyword != null && keyword != "")
			{
				if (titleOnlyKeywordSearch == true)
				{
					bills = bills.Where(b => b.Title.Contains(keyword));
				}
				else
				{
					bills = bills.Where(b => b.IndexedContent.Contains(keyword) || b.Title.Contains(keyword));
				}
			}

			if (fromDate != null && toDate != null)
			{
				bills = bills.Where(b => b.Date >= fromDate)
					.Where(b => b.Date <= toDate);
			}

			if (parliamentNumber != null)
			{
				bills = bills.Where(b => b.Parliament.Number == parliamentNumber);
			}

			if (status != "") 
			{
				bills = bills.Where(b => b.Status == status);
			}

			if (type != "")
			{
				bills = bills.Where(b => b.Type == type);
			}

			return bills;
		}
		/*
		* UI-02.02-07 Bills by first letter
		*/
		public IQueryable<Bill> BillsByFirstLetter(string firstLetter)
		{
			var bills = _parlInfoEntities.Bills.Where(b => b.Title.StartsWith(firstLetter));
			return bills;
		}
		#endregion

		#region Tariff Proposals
		/*
		* UI-02.02-10 & 11 Tariff Proposals
		*/
		public IQueryable<TariffProposal> TariffProposals (string Type)
		{
			var tariffProposals = _parlInfoEntities.TariffProposals.Where(t => t.Type == Type);

			return tariffProposals;

		}
		#endregion

		#region Bills Digests
		/*
		 * UI-02.02-12 Bills Digests
		 */
		public IQueryable<BillsDigest> BillsDigests()
		{
			var billsdigests = _parlInfoEntities.BillsDigests.AsQueryable();
			return billsdigests;
		}
		#endregion

		#region Publications
		/*
		 * UI Reference UI-08.38 Publications Search Panel
		 */
		public IQueryable<Publication> Publications(
			string keyword,
			bool? titleOnlySearch,
			DateTime? fromDate,
			DateTime? toDate,
			string department)
		{
			var pubs = _parlInfoEntities.Publications.AsQueryable();

			//keyword search
			if (keyword != null && keyword != "")
			{
				if (titleOnlySearch == true)
				{
					pubs = pubs.Where(p => p.Title.Contains(keyword));
				}
				else
				{
					pubs = pubs.Where(p => p.Title.Contains(keyword) || p.IndexedContent.Contains(keyword));
				}
			}
			//dates
			if (fromDate != null && toDate != null)
			{
				pubs = pubs.Where(p => p.Date > fromDate)
						.Where(p => p.Date <= toDate);
			}
			//publishing area (department)
			if (department != null && department != "")
			{
				pubs = pubs.Where(p => p.PublishingArea == department);
			}

			return pubs;
		}

		/*
		 * UI Reference UI-08.37 Library Publications Panel
		 */
		public IQueryable<Publication> LibraryPublications(string keyword,
													string publicationType,
													Nullable<DateTime> fromDate,
													Nullable<DateTime> toDate,
													string keywordSearchType)
		{
			var libpubs = _parlInfoEntities.Publications.AsQueryable();

			// publication type
			if (publicationType != null && publicationType != "All")
			{
				libpubs = libpubs.Where(lp => lp.PublicationType == publicationType);
			}
			else
			{
				libpubs = libpubs.Where(lp => lp.PublishingArea == "Parliamentary Library");
			}

			// author / title only search
			if (keyword != "")
			{
				if (keywordSearchType == "Title")
				{
					libpubs = libpubs.Where(lp => lp.Title.Contains(keyword));
				}
				else if (keywordSearchType == "Author")
				{
					libpubs = libpubs.Where(lp => lp.Authors.Contains(keyword));
				}
				else
				{
					libpubs = libpubs.Where(lp => lp.IndexedContent.Contains(keyword) || lp.Title.Contains(keyword) || lp.Authors.Contains(keyword));
				}
			}

			// dates
			if (fromDate != null && toDate != null)
			{
				libpubs = libpubs.Where(lp => lp.Date > fromDate)
									.Where(lp => lp.Date < toDate);
			}

			return libpubs;
		}
		#endregion

	}
}
